package _022_2_15

type ListNode struct {
	Val  int
	Next *ListNode
}

func removeNthFromEnd(head *ListNode, n int) *ListNode {
	nodes := make([]*ListNode, 0)
	temp := head
	for temp != nil {
		nodes = append(nodes, temp)
		temp = temp.Next
	}

	var delNode *ListNode
	var preDelNode *ListNode

	delNode = nodes[len(nodes)-n]
	if len(nodes)-1-n >= 0 {
		preDelNode = nodes[len(nodes)-n-1]
	}

	if preDelNode == nil {
		return delNode.Next
	}

	preDelNode.Next = preDelNode.Next.Next

	return head

}
